//
//  Values+Values.m
//  ClinicalCollections
//
//  Created by janakiraman gopinath on 2/10/13.
//  Copyright (c) 2013 GOPI. All rights reserved.
//

#import "Values+Values.h"

@implementation Values (Values)

+ (void) values:(NSDictionary *) value
           withType:(NSString *) type
  usingCategory:(Categories *) categories
usingClinicalTrialGov:(ClinicalTrialGov *) clinicalTrialGov
       usingContext:(NSManagedObjectContext *) context
{
    Values      *values;
    values = (Values *)[NSEntityDescription insertNewObjectForEntityForName:@"Values"
                                                             inManagedObjectContext:context];
    
    if ([type isEqual:@"outcome_list"]) {
        ;
    }
    if ([type isEqual:@"baseline"] || [type isEqual:@"outcome_list"]) {
        if (![[value valueForKeyPath:@"attributes.value"] isEqual:[NSNull null]])
            values.value = [[NSNumber alloc] initWithFloat:[[value valueForKeyPath:@"attributes.value"] floatValue]];
        else
            values.value = 0;
    }
    else if ([type isEqual:@"participant_flow"])
    {
        if(![[value valueForKeyPath:@"attributes.count"] isEqual:[NSNull null]])
            values.value = [[NSNumber alloc] initWithFloat:[[value valueForKeyPath:@"attributes.count"] floatValue]];
        else
            values.value = 0;
    }
    values.groupId = [value valueForKeyPath:@"attributes.group_id"];
    if (![[value valueForKeyPath:@"attributes.lower_limit"] isEqual:[NSNull null]])
         values.lower_limit = [[NSNumber alloc] initWithFloat:[[value valueForKeyPath:@"attributes.lower_limit"] floatValue]];
    if (![[value valueForKeyPath:@"attributes.upper_limit"] isEqual:[NSNull null]])
        values.upper_limit = [[NSNumber alloc] initWithFloat:[[value valueForKeyPath:@"attributes.upper_limit"] floatValue]];
    if (![[value valueForKeyPath:@"attributes.spread"] isEqual:[NSNull null]])
        values.spread = [[NSNumber alloc] initWithFloat:[[value valueForKeyPath:@"attributes.spread"] floatValue]];
    
    values.categories = categories;
    
    NSArray *groups = [[clinicalTrialGov groups] allObjects];
    
    for (Groups *group in groups) 
        if ([group.groupId isEqual:values.groupId])
            values.groups = group;
    
    //NSLog(@"Values Inserted %@" , values);
    
}

@end
